package cn.hairui.blog.controller;

import cn.hairui.blog.configure.WebLog;
import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.entity.Artical;
import cn.hairui.blog.entity.Myinfo;
import cn.hairui.blog.entity.Tag;
import cn.hairui.blog.service.*;
import cn.hairui.blog.util.DateUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author laoganbu
 * @since 2020-04-06
 */
@Controller
public class TagController {

    @Autowired
    private TagService tagService;
    @Autowired
    private MyinfoService myinfoService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private ArticalService articalService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private VisitrecordService visitrecordService;
    @Autowired
    private ArticaltagService articaltagService;


    //// ==========================前台==============================///////
    @RequestMapping(value = "/tags", method = RequestMethod.GET)
    @WebLog(description = "标签")
    public String tags(HttpServletRequest request, Model model) {
        // 我的信息
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);

        // 分类信息
        List<Map> typesList = typeService.selectByShowFlag(PubConstant.YES_NO_Y, PubConstant.INDEX_TOP_TYPE_COUNT);
        model.addAttribute("types", typesList);

        // 最新推荐
        List<Artical> newList = articalService.listTop(PubConstant.INDEX_TOP_NEWARTICAL_COUNT, "create_time");
        model.addAttribute("newarticals", newList);

        // 本站信息
        int artical_count = articalService.count();// 文章总数
        int type_count = typeService.count();// 分类总数
        int tag_count = tagService.count();// 标签总数
        int comment_count = messageService.count();
        int visit_count = visitrecordService.count();
        int today_visit_count = visitrecordService.countByDate(DateUtil.getCurrentDate());


        Map webinfo = new HashMap();
        webinfo.put("artical_count", artical_count);
        webinfo.put("type_count", type_count);
        webinfo.put("tag_count", tag_count);
        webinfo.put("comment_count", comment_count);
        webinfo.put("visit_count", visit_count);
        webinfo.put("today_visit_count", today_visit_count);

        model.addAttribute("webinfo", webinfo);

        // 标签信息
        List<Map> tagList = tagService.selectTagsByShowFlag(PubConstant.YES_NO_Y);
        model.addAttribute("tags", tagList);

        // 分页信息 文章信息
        Integer pageNum = null;
        String pageNumStr = request.getParameter("pageIndex");
        if (pageNumStr != null) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        int pageSize = PubConstant.INDEX_PAGE_SIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<Map> articalList = new ArrayList<>();

        articalList = articalService.selectByShowFlag(PubConstant.YES_NO_Y);// 默认只展示站长开放的内容
        PageInfo<Map> pageInfo = new PageInfo<Map>(articalList);
        model.addAttribute("articals", articalList);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageIndex", pageNum);
        model.addAttribute("pageSize", pageSize);

        return "tags";
    }

    @RequestMapping(value = "/tag/{id}", method = RequestMethod.GET)
    @WebLog(description = "标签内容")
    public String tag(@PathVariable int id, HttpServletRequest request, Model model) {
        // 我的信息
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);

        // 标签信息
        List<Map> tagList = tagService.selectTagsByShowFlag(PubConstant.YES_NO_Y);
        model.addAttribute("tags", tagList);

        // 最新推荐
        List<Artical> newList = articalService.listTop(PubConstant.INDEX_TOP_NEWARTICAL_COUNT, "create_time");
        model.addAttribute("newarticals", newList);

        // 本站信息
        int artical_count = articalService.count();// 文章总数
        int type_count = typeService.count();// 分类总数
        int tag_count = tagService.count();// 标签总数
        int comment_count = messageService.count();
        int visit_count = visitrecordService.count();
        int today_visit_count = visitrecordService.countByDate(DateUtil.getCurrentDate());

        Map webinfo = new HashMap();
        webinfo.put("artical_count", artical_count);
        webinfo.put("type_count", type_count);
        webinfo.put("tag_count", tag_count);
        webinfo.put("comment_count", comment_count);
        webinfo.put("visit_count", visit_count);
        webinfo.put("today_visit_count", today_visit_count);

        model.addAttribute("webinfo", webinfo);

        // 分类信息
        List<Map> typesList = typeService.selectByShowFlag(PubConstant.YES_NO_Y, PubConstant.INDEX_TOP_TYPE_COUNT);
        model.addAttribute("types", typesList);

        // 分页信息 文章信息
        Integer pageNum = null;
        String pageNumStr = request.getParameter("pageIndex");
        if (pageNumStr != null) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        int pageSize = PubConstant.INDEX_PAGE_SIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<Map> articalList = articalService.selectByShowFlagTagId(PubConstant.YES_NO_Y, id);// 默认只展示站长开放的内容
        PageInfo<Map> pageInfo = new PageInfo<Map>(articalList);
        model.addAttribute("articals", articalList);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageIndex", pageNum);
        model.addAttribute("pageSize", pageSize);

        model.addAttribute("tagId", id);
        return "tags";
    }

    //// ==========================后台==============================///////
    // 列表页面
    @RequestMapping(value = "/manage/tag/list", method = RequestMethod.GET)
    public String list() {
        return "page/tag/list";
    }

    // 新增页面
    @RequestMapping(value = "/manage/tag/add")
    public String add() {
        return "page/tag/add";
    }

    // 列表数据
    @RequestMapping(value = "/manage/tag/data")
    @ResponseBody
    public String data(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        int pageSize = 5;// 默认每页显示5条记录
        int pageNum = 1;
        try {
            pageNum = Integer.parseInt(request.getParameter("page"));
            pageSize = Integer.parseInt(request.getParameter("limit"));
        } catch (Exception e) {
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Map> typeList = tagService.list();

        PageInfo<Map> pageInfo = new PageInfo<Map>(typeList);
        map.put("data", typeList);
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        return JSONUtils.toJSONString(map);
    }

    // 新增数据
    @RequestMapping(value = "/manage/tag/addData")
    @ResponseBody
    @Transactional
    public String add(HttpServletRequest request, Tag tag) {
        Map map = new HashMap<Object, Object>();
        // 通过名称判断是否已经存在
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", tag.getName());

        Tag result = tagService.getOne(queryWrapper);
        if (result != null) {
            map.put(PubConstant.flag, PubConstant.exsit);
            return JSONUtils.toJSONString(map);
        }

        tagService.save(tag);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    // 删除数据
    @RequestMapping(value = "/manage/tag/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public String delete(@Param(value = "id") int id, HttpServletRequest request) {
        Map map = new HashMap();
        // 管理员
        Tag tag = new Tag();
        tag.setId(id);
        tagService.removeById(id);
        articaltagService.removeByTagId(id);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

}
